<template>
  <div class="productcard-container">
    <div class="card" @click="handleClick(data)">
      <div class="img">
        <img :src="data.imgUrl" alt="" />
      </div>
      <div class="desc">{{ data.name }}</div>
      <div class="price">{{ data.price }}</div>
      <div class="addButtom">
        <add-button @click.native.stop="handleAddToCompare(data)"></add-button>
      </div>
    </div>
    <drawer-detail :openDrawer="openDrawer" :data="drawerData" @close="handleClose" />
  </div>
</template>

<script>
import AddButton from "./AddButton.vue";
import DrawerDetail from "./DrawerDetail.vue";
export default {
  components: {
    AddButton,
    DrawerDetail,
  },
  props: ["data"],
  data() {
    return {
      openDrawer: false,
      drawerData: null,
    };
  },
  methods: {
    handleClick(val) {
      // this.$router.push({
      //   name: "Detail",
      //   params: {
      //     val,
      //   },
      // });
      this.openDrawer = true;
      this.drawerData = val;
    },
    handleClose(val) {
      this.openDrawer = val;
    },

    handleAddToCompare(val) {
      const storeLength = this.$store.state.phones.length;
      if (storeLength > 3) {
        //提示不能再添加了
        this.$message({
          message: "对比数据已满！",
          type: "warning",
        });
      } else {
        this.$message({
          message: "已添加进对比列表！",
          type: "success",
        });
        this.$store.commit("addPhone", val);
      }
    },
  },
};
</script>
<style lang="less" scoped>
.card {
  cursor: pointer;
  width: 250px;
  height: 300px;
  border: 1px solid rgb(249, 249, 249);
  display: block;
  padding-top: 15px;

  .img {
    width: 180px;
    height: 180px;
    margin: 5px auto;
    display: flex;
    justify-content: center;

    img {
      width: 80%;
      object-fit: cover;
    }
  }

  .desc {
    font-size: 15px;
    font-weight: bold;
    color: rgb(95, 94, 94);
    margin: 5px auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .price {
    color: orangered;
  }

  .addButtom {
    margin-top: 5px;
  }
}
</style>
